package com.ebelter.btcomlib.utils;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.regex.Pattern;


public class NumUtils {

    public static final DecimalFormat df2 = new DecimalFormat("0.00");

    /**
     * 把字符串转换为int
     */
    public static int string2Int(String numStr) {
        return string2Int(numStr, 0);
    }

    public static int string2Int(String numStr, int defaultValue) {
        if (TextUtils.isEmpty(numStr)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(numStr);
        } catch (Exception e) {
            return defaultValue;
        }
    }


    //一个整数转换成16进制字符串
    public static String toHexString(int num) {
        try {
            return Integer.toHexString(num);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 把字符串转换为int
     */
    public static float string2Float(String numStr) {
        if (TextUtils.isEmpty(numStr)) {
            return 0;
        }
        try {
            return Float.parseFloat(numStr);
        } catch (Exception e) {
            return 0;
        }
    }

    public static long string2Long(String numStr) {
        if (TextUtils.isEmpty(numStr)) {
            return 0;
        }
        try {
            return Long.parseLong(numStr);
        } catch (Exception e) {
            return 0;
        }
    }


    public static String disFlZero(float value, String replaceZero) {
        if (value == 0) {
            return replaceZero;
        }
        return value + "";
    }

    public static String disIntZero(int value, String replaceZero) {
        if (value == 0) {
            return replaceZero;
        }
        return value + "";
    }

    public static float granthanZero(float value) {
        if (value < 0) {
            value = 0;
        }
        return value;
    }


    public static float g0Bl1(float value) {
        return numBaoLiuWei1(granthanZero(value));
    }

    public static int g0Bl0(float value) {
        return (int) granthanZero(value);
    }


    //数字保留位
    public static int numBaoLiuWei0(float value) {
        return (int) ((Math.round(value * 1)) / 1.0);//如果要求精确4位就*10000然后/10000
    }

    //数字保留位
    public static float numBaoLiuWei1(float value) {
        return (float) ((Math.round(value * 10)) / 10.0);
    }

    public static float numBaoLiuWei2(float value) {
        return (float) ((Math.round(value * 100)) / 100.0);
    }
    public static float numBaoLiuWei6(double value) {
        return (float) ((Math.round(value * 1000000)) / 1000000.0);
    }

    public static String floatDis2XSH(float value) {
        return df2.format(value);
    }

    public static String floatDis2XSH_2(float value, String str2) {
        if (value <= 0) {
            return str2;
        }
        return df2.format(value);
    }

    public static float numBaoLiuWei2_down(float value) {
        return  (float)(((int) (value * 100))*1.0 / 100.0);
    }

    public static float numBaoLiuWeiDown1(float value){
        int t= (int) (value*10);
        value= (float) (t*1.0/10.0);
        value=numBaoLiuWei1(value);
        return value;
    }

    private static Random random;

    //随机一个数
    //baoliuwei为负数时保留原生的数值
    public static float randomF(float min, float max) {

        float p = getRandom().nextFloat();
        return (max - min) * p + min;
    }



    private static Random getRandom() {
        if (random == null) {
            random = new Random();
        }
        return random;
    }


    //16进制字符串转换为10进制数
    public static int hexStr2dec(String hexStr) {
        return Integer.parseInt(hexStr.replaceAll("^0[x|X]", ""), 16);
    }


    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
}
